/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OKe.java
 *
 * Created on Nov 19, 2012, 2:03:13 PM
 */
package rploo_ciayoo;

import Dosen.Dosen;
import Dosen.DosenKontrol;
import Dosen.DosenTableModel;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Rico
 */
public class Master_Dosen extends javax.swing.JInternalFrame {

    /**
     * Creates new form OKe
     */
    public Master_Dosen() {
        initComponents();
        lihatSemua();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        IdDosenTxt = new javax.swing.JTextField();
        NamaDosenTxt = new javax.swing.JTextField();
        simpanDosenBtn = new javax.swing.JButton();
        updateDosenBtn = new javax.swing.JButton();
        deleteDosenBtn = new javax.swing.JButton();
        batalDosenBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelDosen = new javax.swing.JTable();
        keluarDosenBtn = new javax.swing.JButton();

        jLabel1.setText("Id Dosen ");

        jLabel2.setText("Nama Dosen");

        simpanDosenBtn.setText("Simpan");
        simpanDosenBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanDosenBtnActionPerformed(evt);
            }
        });

        updateDosenBtn.setText("Update");
        updateDosenBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateDosenBtnActionPerformed(evt);
            }
        });

        deleteDosenBtn.setText("Delete");
        deleteDosenBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteDosenBtnActionPerformed(evt);
            }
        });

        batalDosenBtn.setText("Batal");
        batalDosenBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalDosenBtnActionPerformed(evt);
            }
        });

        tabelDosen.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Id Dosen", "Nama Dosen"
            }
        ));
        tabelDosen.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelDosenMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelDosen);

        keluarDosenBtn.setText("Keluar");
        keluarDosenBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluarDosenBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1)
                .addGap(74, 74, 74)
                .addComponent(IdDosenTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel2)
                .addGap(60, 60, 60)
                .addComponent(NamaDosenTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(simpanDosenBtn)
                .addGap(20, 20, 20)
                .addComponent(updateDosenBtn)
                .addGap(20, 20, 20)
                .addComponent(deleteDosenBtn)
                .addGap(20, 20, 20)
                .addComponent(batalDosenBtn))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(358, 358, 358)
                .addComponent(keluarDosenBtn))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel1))
                    .addComponent(IdDosenTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jLabel2))
                    .addComponent(NamaDosenTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(simpanDosenBtn)
                    .addComponent(updateDosenBtn)
                    .addComponent(deleteDosenBtn)
                    .addComponent(batalDosenBtn))
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(keluarDosenBtn)
                .addContainerGap(49, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void batalDosenBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalDosenBtnActionPerformed
        // TODO add your handling code here:
        IdDosenTxt.setText(null);
        NamaDosenTxt.setText(null);
}//GEN-LAST:event_batalDosenBtnActionPerformed

    private void keluarDosenBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluarDosenBtnActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_keluarDosenBtnActionPerformed

    private void simpanDosenBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanDosenBtnActionPerformed
        // TODO add your handling code here:
        Dosen dosenBaru = new Dosen();
        String id = IdDosenTxt.getText();
        String nama = NamaDosenTxt.getText();
        dosenBaru.setId_dosen(id);
        dosenBaru.setNama_dosen(nama);
        try {
            DosenKontrol kontrol;
            kontrol = DosenKontrol.getKoneksi();
            kontrol.tambahDosen(dosenBaru);
            JOptionPane.showMessageDialog(this, "Data Tersimpan");
            IdDosenTxt.setText("");
            NamaDosenTxt.setText("");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data Gagal Disimpan!!! \n Mohon Ulangi");
            Logger.getLogger(Master_Dosen.class.getName()).log(Level.SEVERE, null, ex);
        }
        lihatSemua();
    }//GEN-LAST:event_simpanDosenBtnActionPerformed

    private void updateDosenBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateDosenBtnActionPerformed
        // TODO add your handling code here
        Dosen dosenBaru = new Dosen();
        String id = IdDosenTxt.getText();
        String nama = NamaDosenTxt.getText();
        dosenBaru.setId_dosen(id);
        dosenBaru.setNama_dosen(nama);
        try {
            DosenKontrol kontrol;
            kontrol = DosenKontrol.getKoneksi();
            kontrol.updateDosen(dosenBaru);
            JOptionPane.showMessageDialog(this, "Data Terupdate");
            IdDosenTxt.setText("");
            NamaDosenTxt.setText("");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data Gagal Terupdate!!! \n Mohon Ulangi");
            Logger.getLogger(Master_Dosen.class.getName()).log(Level.SEVERE, null, ex);
        }
        lihatSemua();
    }//GEN-LAST:event_updateDosenBtnActionPerformed

    private void deleteDosenBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteDosenBtnActionPerformed
        // TODO add your handling code here:
        Dosen dosenBaru = new Dosen();
        String id = IdDosenTxt.getText();
        String nama = NamaDosenTxt.getText();
        dosenBaru.setId_dosen(id);
        dosenBaru.setNama_dosen(nama);
        try {
            DosenKontrol kontrol;
            kontrol = DosenKontrol.getKoneksi();
            kontrol.hapusPrepared(dosenBaru);
            JOptionPane.showMessageDialog(this, "Data Terhapus");
            IdDosenTxt.setText("");
            NamaDosenTxt.setText("");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data Gagal Terhapus! \n Mohon Ulangi");
            Logger.getLogger(Master_Dosen.class.getName()).log(Level.SEVERE, null, ex);
        }
        lihatSemua();
    }//GEN-LAST:event_deleteDosenBtnActionPerformed

    private void tabelDosenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelDosenMouseClicked
        // TODO add your handling code here:
        int row = tabelDosen.getSelectedRow();
        String id = tabelDosen.getValueAt(row, 0).toString();
        String nama = tabelDosen.getValueAt(row, 1).toString();
        IdDosenTxt.setText(id);
        NamaDosenTxt.setText(nama);
        simpanDosenBtn.setEnabled(true);
    }//GEN-LAST:event_tabelDosenMouseClicked

    public void lihatSemua() {
        IdDosenTxt.requestFocus();
        try {
            List<Dosen> dos = DosenKontrol.getKoneksi().lihatDataDosen();
            DosenTableModel table = new DosenTableModel(dos);
            tabelDosen.setModel(table);
        } catch (SQLException ex) {
            Logger.getLogger(Master_Dosen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField IdDosenTxt;
    private javax.swing.JTextField NamaDosenTxt;
    private javax.swing.JButton batalDosenBtn;
    private javax.swing.JButton deleteDosenBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton keluarDosenBtn;
    private javax.swing.JButton simpanDosenBtn;
    private javax.swing.JTable tabelDosen;
    private javax.swing.JButton updateDosenBtn;
    // End of variables declaration//GEN-END:variables
}
